package edu.spsu;

import edu.spsu.utils.Util;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

public class LoginActivity extends Activity {
private static String TAG = LoginActivity.class.getName();
	
	private EditText usernameField;
	private EditText passwordField;
	private CheckBox saveCredentialsField;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_login);
        
        usernameField = (EditText)findViewById(R.id.usernameEditText);
        passwordField = (EditText)findViewById(R.id.passwordEditText);
        saveCredentialsField = (CheckBox)findViewById(R.id.saveCredentialsCheckbox);
        
        SharedPreferences sp = Util.getPref(this);
        boolean checked = sp.getBoolean("saveCredentials", false);
        
		String user = sp.getString("user", "");
		String pass = sp.getString("pass", "");

        usernameField.setText(user);
        passwordField.setText(pass);
        saveCredentialsField.setChecked(checked);
        
    }
    
    public void loginClicked(View v) {
    	String username = usernameField.getText().toString();
    	String password = passwordField.getText().toString();
    	boolean saveCredentials = saveCredentialsField.isChecked();
    	
    	Util.setUpUser(this, username, password, saveCredentials);
    	
    	// Once the service is up, we will use it to check the credentials
    	// For now, though, we will just go straight to the main activity
    	Intent myIntent = new Intent(this, SPSUActivity.class);
    	myIntent.putExtra("user", username);
    	myIntent.putExtra("pass", password);
    	startActivity(myIntent);
    }
}
